package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.domain.Page;
import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import cn.sciento.boot.platform.lov.annotation.ProcessLovValue;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.api.dto.DataChkBatchCreateDTO;
import cn.sciento.transfer.app.service.DataChkBatchService;
import cn.sciento.transfer.domain.entity.DataChkBatch;
import cn.sciento.transfer.domain.entity.DataChkBatchDtl;
import cn.sciento.transfer.domain.entity.DataChkBatchLine;
import cn.sciento.transfer.domain.repository.DataChkBatchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController("dataChkBatchController.v1")
@RequestMapping({"/v1/{organizationId}/data-chk-batchs"})
public class DataChkBatchController extends BaseController {
  @Autowired
  private DataChkBatchRepository dataChkBatchRepository;
  
  @Autowired
  private DataChkBatchService dataChkBatchService;
  
  @ApiOperation("查询数据核对批次")
  @Permission(level = ResourceLevel.ORGANIZATION)
  @GetMapping
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<DataChkBatchLine>> list(@Encrypt DataChkBatchLine dataChkBatchLine, @ApiIgnore @SortDefault(value = {"dataChkBatchId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    Page<DataChkBatchLine> list = this.dataChkBatchRepository.pageDataChk(pageRequest, dataChkBatchLine);
    return Results.success(list);
  }
  
  @ApiOperation("数据核对头详情查询")
  @Permission(level = ResourceLevel.ORGANIZATION)
  @GetMapping({"/{dataChkBatchLineId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<DataChkBatchLine> detailHeader(@Encrypt @PathVariable("dataChkBatchLineId") Long dataChkBatchLineId) {
    return Results.success(this.dataChkBatchService.detailDataChk(dataChkBatchLineId));
  }
  
  @ApiOperation("数据核对确认")
  @Permission(level = ResourceLevel.ORGANIZATION)
  @PostMapping
  public ResponseEntity<DataChkBatch> create(@Encrypt @RequestBody DataChkBatchCreateDTO dataChkBatchCreateDTO) {
    return Results.success(this.dataChkBatchService.createChk(dataChkBatchCreateDTO));
  }
  
  @ApiOperation("数据核对批次明细")
  @Permission(level = ResourceLevel.ORGANIZATION)
  @GetMapping({"/{dataChkBatchId}/{dataChkBatchLineId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<DataChkBatchDtl>> detail(@Encrypt @PathVariable Long dataChkBatchId, @Encrypt @PathVariable Long dataChkBatchLineId, @ApiIgnore @SortDefault(value = {"dataChkDtlId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    return Results.success(this.dataChkBatchService.pageDataCheckDtl(dataChkBatchId, dataChkBatchLineId, pageRequest));
  }
}
